package com.book.servlet.api;

import com.book.entity.Book;
import com.book.entity.Student;
import com.book.entity.User;
import com.book.service.BookService;
import com.book.service.StudentService;
import com.book.service.impl.BookServiceImpl;
import com.book.service.impl.StudentServiceImpl;
import com.book.util.ThymeleafUtil;
import org.thymeleaf.context.Context;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 **@Author:youpeng
 **Created on 2022/4/26
 */
@WebServlet("/updateBook")
public class UpdateBookServlet extends HttpServlet {

    BookService bookService=new BookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Context context = new Context();
        User user = (User) req.getSession().getAttribute("user");
        context.setVariable("nickname",user.getNickname());
        String title = req.getParameter("t");
        String desc = req.getParameter("d");
        String bid = req.getParameter("id");
        String price = req.getParameter("p");
        context.setVariable("title",title);
        context.setVariable("desc",desc);
        context.setVariable("price",price);
        context.setVariable("bid",bid);
        ThymeleafUtil.process("updateBook.html",context, resp.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String title = req.getParameter("title");
        String desc = req.getParameter("desc");
        double price = Double.parseDouble(req.getParameter("price"));
        int bid= Integer.parseInt(req.getParameter("bid"));
        bookService.updateStudent(new Book(bid,title,desc,price));
        resp.sendRedirect("books");
    }
}
